<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in feedback_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable>
          <el-option
            v-for="dict in process_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeCreateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['feedback:feedback:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['feedback:feedback:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['feedback:feedback:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['feedback:feedback:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="feedback_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" width="120">
        <template #default="scope">
          <el-button link type="primary" @click="handleViewDetail(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="photoUrl" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.photoUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="process_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['feedback:feedback:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['feedback:feedback:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改反馈管理对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="feedbackRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in feedback_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="图片" prop="photoUrl">
          <image-upload v-model="form.photoUrl"/>
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择处理状态">
            <el-option
              v-for="dict in process_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
        v-model="detailVisible"
        title="反馈详情"
        width="680px"
        class="detail-dialog"
        append-to-body
    >
      <div class="detail-content">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="反馈类型">
            <dict-tag :options="feedback_type" :value="currentDetail.type"/>
          </el-descriptions-item>
          <el-descriptions-item label="处理状态">
            <dict-tag :options="process_status" :value="currentDetail.status"/>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ parseTime(currentDetail.createTime, '{y}-{m}-{d} {h}:{i}') }}
          </el-descriptions-item>
          <el-descriptions-item label="更新时间">
            {{ parseTime(currentDetail.updateTime, '{y}-{m}-{d} {h}:{i}') }}
          </el-descriptions-item>
        </el-descriptions>

        <div class="content-section">
          <h3 class="section-title">反馈内容</h3>
          <div
              class="formatted-content"
              v-html="formattedContent"
          ></div>
        </div>

        <div class="image-section" v-if="currentDetail.photoUrl">
          <h3 class="section-title">相关图片</h3>
          <image-preview
              :src="currentDetail.photoUrl"
              :preview-src-list="[currentDetail.photoUrl]"
              style="max-width: 100%"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Feedback">
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback } from "@/api/feedback/feedback";
import { marked } from 'marked';
import DOMPurify from 'dompurify';

const { proxy } = getCurrentInstance();
const { feedback_type, process_status } = proxy.useDict('feedback_type', 'process_status');

const feedbackList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: null,
    status: null,
    createTime: null,
  },
  rules: {
    type: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    content: [
      { required: true, message: "内容不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
const detailVisible = ref(false);
const currentDetail = ref({});

// 格式化内容显示
const formattedContent = computed(() => {
  if (!currentDetail.value.content) return '';
  const rawHtml = marked.parse(currentDetail.value.content);
  return DOMPurify.sanitize(rawHtml);
});

// 查看详情处理
const handleViewDetail = (row) => {
  currentDetail.value = { ...row };
  detailVisible.value = true;
};
/** 查询反馈管理列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listFeedback(queryParams.value).then(response => {
    feedbackList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    type: null,
    content: null,
    photoUrl: null,
    status: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("feedbackRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加反馈管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFeedback(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改反馈管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["feedbackRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFeedback(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFeedback(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除反馈管理编号为"' + _ids + '"的数据项？').then(function() {
    return delFeedback(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('feedback/feedback/export', {
    ...queryParams.value
  }, `feedback_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style lang="scss" scoped>
.detail-dialog {
  :deep(.el-dialog__header) {
    border-bottom: 1px solid #ebeef5;
    margin-right: 0;
    padding: 15px 20px;
  }

  :deep(.el-dialog__body) {
    padding: 20px;
  }

  .detail-content {
    max-height: 70vh;
    overflow-y: auto;

    .content-section {
      margin-top: 20px;

      .formatted-content {
        line-height: 1.6;
        font-size: 14px;
        color: #606266;

        h1, h2, h3 {
          color: #303133;
          margin: 1em 0 0.5em;
        }

        img {
          max-width: 100%;
          height: auto;
          margin: 10px 0;
          border-radius: 4px;
          box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
        }

        pre {
          background: #f6f8fa;
          padding: 16px;
          border-radius: 4px;
          overflow-x: auto;
        }
      }
    }

    .image-section {
      margin-top: 20px;

      :deep(.el-image) {
        width: 100%;
        border-radius: 4px;
        border: 1px solid #ebeef5;
      }
    }

    .section-title {
      color: #303133;
      font-size: 15px;
      margin: 0 0 12px 0;
      padding-bottom: 8px;
      border-bottom: 1px dashed #ebeef5;
    }

    .el-descriptions {
      margin-bottom: 20px;

      :deep(.el-descriptions__label) {
        width: 100px;
        color: #909399;
      }
    }
  }
}
</style>